layui.config({
	base : "js/"
}).use(['flow','form','layer'],function(){
    var flow = layui.flow,
        form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;
	var fileBtn = $("input[type=file]");
	     fileBtn.on("change", function(){
	         var index = $(this).val().lastIndexOf("\\");
	         var sFileName = $(this).val().substr((index+1));
	         $("#rightText").html(sFileName);
	     });
		
		//执行实例
	  function upload(){
                var file =  document.getElementById("file").files[0];
            }
            $(function(){
                $("#save_image").click(function(){
                   	var $checkbox = $('#Images li input[type="checkbox"]');
			        var $checked = $('#Images li input[type="checkbox"]:checked');
			        if($checkbox.is(":checked")){
			                    if($checked.length>1)
			                    {
			                    	layer.msg("请选择一个图片！",{icon:0});
			                    	return;
			                    }
			                    var title = "";
			                    $checked.each(function(){
			                        title = $(this).attr("title");
			                        
			                    })
			                    window.parent.$(".imgUrl").val(title);
			                     var parentIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引
			                    parent.layer.close(parentIndex);
			                    
			        }else{
			            layer.msg("请至少选择一个图片");
			        }
                });
            })

    //流加载图片
    var imgNums = 15;  //单页显示图片数量
//  flow.load({
//      elem: '#Images', //流加载容器
//      done: function(page, next){ //加载下一页
//          $.get("../../json/images.json",function(data){
//              //模拟插入
//              var imgList = [];
//              var maxPage = imgNums*page < data.length ? imgNums*page : data.length;
//              setTimeout(function(){
//                  for(var i=imgNums*(page-1); i<maxPage; i++){
//                      imgList.push('<li><img src="'+ data[i].imgSrc +'"><div class="operate"><div class="check"><input type="checkbox" name="belle" lay-filter="choose" lay-skin="primary" title="'+data[i].imgTitle+'"></div><i class="layui-icon img_del">&#xe640;</i></div></li>')
//                  }
//                  next(imgList.join(''), page < (data.length/imgNums));
//                  form.render();
//              }, 500);
//          }); 
//      }
//  });
    flow.load({
        elem: '#Images', //流加载容器
        done: function(page, next){ //加载下一页
            $.ajax({
			        url: API_URL_HEAD+"file/getAllImageName",
			        type: 'get',
			        async: false,
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	if(data.code == 0){
		              		 //刷新父页面
		              		 var dataArray = [];
		              		 if(data.data!=null && data.data!="")
		              		 {
		              		 	dataArray = data.data.split(",");
		              		 }
			 				var imgList = [];
			                var maxPage = imgNums*page < dataArray.length ? imgNums*page : dataArray.length;
			                setTimeout(function(){
			                    for(var i=imgNums*(page-1); i<maxPage; i++){
			                        imgList.push('<li><img src="'+API_URL_HEAD+dataArray[i] +'"><div class="operate"><div class="check"><input type="checkbox" name="belle" lay-filter="choose" lay-skin="primary" title="'+dataArray[i]+'"></div></div></li>')
			                    }
			                    next(imgList.join(''), page < (dataArray.length/imgNums));
			                    form.render();
			                }, 500);
		            	}
			        }
			  });
        }
    });

    //删除单张图片
    $("body").on("click",".img_del",function(){
        var _this = $(this);
        layer.confirm('确定删除当前选中图片"'+_this.siblings().find("input").attr("title")+'"吗？',{icon:3, title:'提示信息'},function(index){
            
            var delIdArray = [];
            delIdArray.push(_this.siblings().find("input").attr("title"));
            $.ajax({
			        url: API_URL_HEAD+"file/deleteFile",
			        type: 'POST',
			        async: false,
			        contentType: 'application/json; charset=utf-8',
			        data:JSON.stringify(delIdArray),
			        dataType: 'json',
			        success: function (data) {
			        	if(data.code == 0){
		              		layer.msg(data.data);
		            	}
			        }
			  });
            
            _this.parents("li").hide(1000);
            //setTimeout(function(){_this.parents("li").remove();},950);
            layer.close(index);
        });
    })

    //全选
    form.on('checkbox(selectAll)', function(data){
        var child = $("#Images li input[type='checkbox']");
        child.each(function(index, item){
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });

    //通过判断文章是否全部选中来确定全选按钮是否选中
//  form.on("checkbox(choose)",function(data){
//      var child = $(data.elem).parents('#Images').find('li input[type="checkbox"]');
//      var childChecked = $(data.elem).parents('#Images').find('li input[type="checkbox"]:checked');
//      if(childChecked.length == child.length){
//          $(data.elem).parents('#Images').siblings("blockquote").find('input#selectAll').get(0).checked = true;
//      }else{
//          $(data.elem).parents('#Images').siblings("blockquote").find('input#selectAll').get(0).checked = false;
//      }
//      form.render('checkbox');
//  })

    //批量删除
    $(".batchDel").click(function(){
        var $checkbox = $('#Images li input[type="checkbox"]');
        var $checked = $('#Images li input[type="checkbox"]:checked');
        if($checkbox.is(":checked")){
            layer.confirm('确定删除选中的图片？',{icon:3, title:'提示信息'},function(index){
                var index = layer.msg('删除中，请稍候',{icon: 16,time:false,shade:0.8});
                setTimeout(function(){
                    //删除数据
                    $checked.each(function(){
                        $(this).parents("li").hide(1000);
                        setTimeout(function(){$(this).parents("li").remove();},950);
                    })
                    $('#Images li input[type="checkbox"]').prop("checked",false);
                    form.render();
                    layer.close(index);
                    layer.msg("删除成功");
                },2000);
            })
        }else{
            layer.msg("请选择需要删除的图片");
        }
    })

})